package main

import (
	"context"
	"log"
	"net"

	pb "gitee.com/dingdang4D/go-basic-exercises/net/grpc/proto/calculator"

	"google.golang.org/grpc"
)

// 定义一个结构体来实现 CalculatorServer 接口
type server struct {
	pb.UnimplementedCalculatorServer
}

// 实现 Add 方法
func (s *server) Add(ctx context.Context, req *pb.AddRequest) (*pb.AddResponse, error) {
	result := req.A + req.B
	return &pb.AddResponse{Result: result}, nil
}

func main() {
	lis, err := net.Listen("tcp", ":50051")
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	s := grpc.NewServer()
	pb.RegisterCalculatorServer(s, &server{})
	log.Printf("server listening at %v", lis.Addr())
	if err := s.Serve(lis); err != nil {
		log.Fatalf("failed to serve: %v", err)
	}
}
